package com.bsj.media.media.packageFlv;


import com.bsj.media.media.ParserVideoData;
import com.bsj.power.common.def.protocol.rtp.RtpParameterSet;
import lombok.extern.slf4j.Slf4j;


import java.nio.ByteBuffer;

/**
 * @Description 视频flv处理
 * @Date 2022/10/25 14:48
 **/
@Slf4j
public class VideoFlvHandler extends FlvTypeHandler{

    public CacheParams cacheParamsNow;

    public long lastTime; // 发生码流切换时，记录接收到数据的时间，如果超过3秒，就当做切换成功
    public VideoFlvHandler(CacheParams cacheParams) {
        super((byte) 0x01);
        cacheParamsNow = cacheParams;
    }

    @Override
    public boolean preJudgement(byte[] fdBody) {
        if (!ParserVideoData.checkIframe(fdBody)) {
            // 如果是只有音频的时候不判断这个
//            log.warn("打包头---不是I帧，丢掉, 帧类型："+ ParserVideoData.getTypeFrame2(fdBody) +"---key:" );
            return false;
        }
        int videoRate = ParserVideoData.getVideoFrame(fdBody, 0);
        if (videoRate > 0) {
            super.st_videoRate = 1000 / videoRate;
        }
        // 获取视频分辨率
        int solutionRatio = ParserVideoData.getVideoSolution(fdBody, 0);
        super.getSolutionRatio(ParserVideoData.getVideoSolution(fdBody, 0));

        // 获取帧率
        super.videoFrameRate = ParserVideoData.getVideoFrame(fdBody, 0) != 0 ? ParserVideoData.getVideoFrame(fdBody, 0) : 15;
        super.videocodecid = ParserVideoData.getAudioType(fdBody) == RtpParameterSet.fdVideoCodeTypeH265 ? RtpParameterSet.flvCodecIdH265 : RtpParameterSet.flvCodecIdH264;
        log.info("开始打包: uri" + ", 帧率:" + super.videoFrameRate + ", 视频类型:" + ((super.videocodecid == RtpParameterSet.flvCodecIdH265) ? "h265" : "h264"));
        // 判断高标清切换数据是否有残余
        if (cacheParamsNow.getCodeType() == 1) {
            if (lastTime == 0) {
                lastTime = System.currentTimeMillis();
            }
            // 主码流 D1以上
            if (solutionRatio < 3) {
                if (System.currentTimeMillis() - lastTime < 3000) {
                    log.warn(" 是高清通道，码流分辨率为：" + super.width + ":" + super.height + " solutionRatio="
                            + solutionRatio + " 不正确丢弃！");
                    return false;
                } else {
                    // 这时候认为切换成功：
                    log.info( "切换数据超过3秒，" + super.width + ":" + super.height + "solutionRatio=" + solutionRatio
                            + "切换成功！");
                }
            }
        } else if (cacheParamsNow.getCodeType() == 2) {
            if (lastTime == 0) {
                lastTime = System.currentTimeMillis();
            }
            // 子码流D1以下
            if (solutionRatio >= 3) {
                if (System.currentTimeMillis() - lastTime < 3000) {
                    log.warn(" 是标清通道，码流分辨率为：" + super.width + ":" + super.height + " solutionRatio="
                            + solutionRatio + " 不正确丢弃");
                    return false;
                } else {
                    // 这时候认为切换成功：
                    log.info("切换数据超过3秒，" + super.width + ":" + super.height + "solutionRatio=" + solutionRatio
                            + "切换成功！");
                }
            }
        }
        return true;
    }

    @Override
    public void setFristMediaTag(byte[] fdBody, ByteBuffer dst) {
        // 1、视频Tag[0]打包
        if (fdBody[2] == RtpParameterSet.fdVideoCodeTypeH265) {
            super.h265FristTagFlv(fdBody, dst);
        } else {
            super.h264FristTagFlv(fdBody, dst);
        }
    }

    @Override
    public boolean packVideoFlag() {
        return true;
    }

    @Override
    public boolean packAudioFlag() {
        return false;
    }
}
